package net.jxtz.jxtzos.spider.pipeline;


import lombok.extern.slf4j.Slf4j;
import net.jxtz.jxtzos.entity.spider.SpiderBeike;
import net.jxtz.jxtzos.mapper.redis.RedisMapper;
import net.jxtz.jxtzos.service.spider.ISpiderBeikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

// 贝壳爬取结果处理
@Component
@Slf4j
public class BeiKePipline implements Pipeline {

    @Autowired
    private ISpiderBeikeService beikeService;

    @Autowired
    private RedisMapper redisMapper;

    public static final String CACHE_NAME = "cache.spider.beikeProcessor";

    @Override
    public void process(ResultItems resultItems, Task task) {
        SpiderBeike beike = resultItems.get("beike");
        // 不为空的情况下保存到数据库
        if (beike != null) {
            boolean save = beikeService.save(beike);
            log.info("存入数据库：" + save + "，对象：" + beike);
            redisMapper.leftPush(CACHE_NAME, beike);
        }
    }

}
